
import Pagination from '@/components/pagination';
import mixin from '@/service/mixin';
import AlertDialog from '@/components/alert-dialog';
import { keyMap } from '@/config';
import SelectAppList from '@/components/select-app-list';

export default {
    name: 'customAssistant',
    components: {
        Pagination,
        AlertDialog,
        SelectAppList,
    },
    mixins: [mixin],
    props: {
        activeTab: {
            type: String,
            required: true
        },
        searchForm: {
            type: Object,
            required: true
        },
        isCaptain: {
            type: Boolean,
            required: true
        },
    },
    data() {
        return {
            keyMap,
            thisRoleType: 1,
            form: {
                // auditorId: '',
                userId: '',
                maxNum: ''
            },
            showNumDialog: false,
            rules: {
                maxNum: [
                    { required: true, message: this.$t('authority.everydayCase'), trigger: 'blur' },
                    { pattern: /^\d+$/, message: this.$t('verify.inputInteger'), trigger: 'blur' }
                ]
            },
            showSetGroupDialog: false,
            groupForm: {
                userId: '', // 客服专员id
                username: '',
                leaderId: '', // 新组别Id
            },
            captainList: [],
            user: this.$storage.get('user'),
            // 设置
            showSettingDialog: false,
            settingForm: {
                userId: '',
                appName: [],
                marketingType: [],
            },
            settingRules: {
                appName: [
                    { required: true, message: this.$t('verify.plsChoose') },
                ],
                marketingType: [
                    { required: true, message: this.$t('verify.plsChoose') },
                ],
            },
        };
    },
    watch: {
        activeTab(val) {
            if ('customAssistant' === val) {
                this.page.currentPage = 1;
                this.getTableData();
            }
        },
        'searchForm.search'(val) {
            if (val && this.activeTab === 'customAssistant') {
                this.page.currentPage = 1;
                this.getTableData();
            }
        }
    },
    methods: {
        getTableData(page = this.page) {
            const params = {
                roleId: this.thisRoleType,
                username: this.searchForm.username,
                leaderId: this.isCaptain?this.user.id:'',//如果为电销主管，传userId，不是就不传
                page: {
                    pageNo: page.currentPage,
                    pageSize: this.page.pageSize
                }
            };
            this.tableData = [];
            this.$api.telSales.getAllUserInfo(params).then((res) => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data.data) {
                    data.data.map(item => {
                        item.appNameList = item.appName ? item.appName.split(',') : [];
                        item.marketingTypeList = item.marketingType ? item.marketingType.split(',') : [];
                    });
                    this.tableData = data.data;
                    this.page.recordsTotal = data.pageCount;
                    if (this.searchForm.search) {
                        this.$emit('search', this.searchForm);
                    }
                    this.tableMaxHeight = window.document.body.clientHeight - 230;
                }
            });
        },
        changeStatus(row) {
            const params = {
                userId: row.userId,
                entrySwitch: row.entrySwitch
            };
            this.$api.telSales.updateUserInfo(params).then((res) => {
                const { status, error } = res;
                if ('1' === status && '00000000' === error) {
                    this.$message({
                        message: this.$tc('common.successfulOperation'),
                        showClose: true,
                        duration: 1000,
                        type: 'success',
                    });
                    this.getTableData();
                }
            });
        },
        setNum() {
            this.$refs.form.validate((val) => {
                if (val) {
                    const params = {
                        userId: this.form.userId,
                        maxNum: this.form.maxNum
                    };
                    this.$api.telSales.updateUserInfo(params).then((res) => {
                        const { status, error } = res;
                        if ('1' === status && '00000000' === error) {
                            this.showNumDialog = false;
                            this.$message({
                                message: this.$tc('common.successfulOperation'),
                                showClose: true,
                                duration: 1000,
                                type: 'success',
                            });
                            this.getTableData();
                        }
                    });
                }
            });
        },
        showSetNum(row) {
            this.form.userId = row.userId;
            this.form.maxNum = row.maxNum;
            this.showNumDialog = true;
        },
        showGroupDialog(row) {
            this.groupForm.userId = row.userId; // 客服专员id
            this.groupForm.username = row.username;
            this.groupForm.leaderId = row.leaderId; // 新组别id
            this.showSetGroupDialog = true;
        },
        // 客服专员分组
        setGroup() {
            const params = {
                userId: this.groupForm.userId,
                leaderId: this.groupForm.leaderId
            };
            this.$api.telSales.changeUserGroup(params).then((res) => {
                const { status, error } = res;
                if ('1' === status && '00000000' === error) {
                    this.showSetGroupDialog = false;
                    this.$message({
                        message: this.$tc('common.successfulOperation'),
                        showClose: true,
                        duration: 1000,
                        type: 'success',
                    });
                    this.getTableData();
                }
            });
        },
        // 获取所有的主管
        getCaptainList() {
            const params = {
                roleId: 2,
                username: '',
                leaderId: '',
            };
            this.$api.telSales.getAllUserInfo(params).then((res) => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data.data) {
                    this.captainList = data.data;
                }
            });
        },
        searchAuditOrder(row) {
            this.$emit('getData', { userId: row.userId,username:row.username }, 'customerDistribution');
        },
        // 显示设置弹框
        showSetting(row) {
            this.settingForm.userId = row.userId;
            this.settingForm.appName = row.appNameList;
            this.settingForm.marketingType = row.marketingTypeList;
            this.showSettingDialog = true;
        },
        settingUserInfo() {
            this.$refs.settingForm.validate((val) => {
                if (val) {
                    const { appName, marketingType, userId } = this.settingForm;
                    const params = {
                        userId,
                        appName: appName.join(','),
                        marketingType: marketingType.join(','),
                    };
                    this.$api.telSales.updateUserInfo(params).then((res) => {
                        const { status, error } = res;
                        if ('1' === status && '00000000' === error) {
                            this.showSettingDialog = false;
                            this.$message({
                                message: this.$tc('common.successfulOperation'),
                                showClose: true,
                                duration: 1000,
                                type: 'success',
                            });
                            this.getTableData();
                        }
                    });
                }
            });
        },
    },
    created() {
        if (this.activeTab === 'customAssistant') {
            this.getTableData();
            if (this.captainList.length === 0) {
                this.getCaptainList();
            }
        }
    }
};
